<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello promise</title>
</head>
<body>
  <noscript>You need to enable JavaScript to run this app.</noscript>
  <div>
    <button id="btn">点击抽奖</button>
  </div>

  <script type="text/javascript">
    function rand(m, n){
      return Math.ceil(Math.random()*(n-m+1))+m-1
    }

    const btn = document.getElementById('btn')
    btn.addEventListener('click', function(){
      // setTimeout(()=>{
      //   let n = rand(1, 100)
      //   if(n<=30) alert('中奖了，号码是:'+n)
      //   else alert('再接再厉,号码:'+n)
      // }, 2000)

      const p = new Promise((resolve, reject)=>{
        setTimeout(()=>{
         let n = rand(1, 100)
         if(n<=30) resolve(n)
         else  reject(n)
        }, 2000)
      })

      p.then((n)=>{
        alert('中奖了，号码是:'+n)
      }, (n)=>{
        alert('再接再厉,号码是:'+n)
      })
    })

    let p = new Promise((resolve, reject)=>{
      resolve('ok')
    })
    
    console.log(p)
  </script>
</body>
</html>